package com.atguigu.auth.service.impl;

import com.atguigu.auth.mapper.SysRoleMapper;
import com.atguigu.auth.mapper.SysUserRoleMapper;
import com.atguigu.auth.service.SysRoleService;
import com.atguigu.auth.service.SysUserRoleService;
import com.atguigu.model.system.SysRole;
import com.atguigu.model.system.SysUserRole;
import com.atguigu.vo.system.AssignRoleVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * ClassName SysRoleServiceImpl
 * Package: com.atguigu.auth.service.impl
 * ProjectName guigu-oa-parent
 * Description:
 *
 * @Author: 比特张
 * @Create: 2023/4/17 - 17:21
 * @Version: v1.0
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
    @Resource
    private SysUserRoleService sysUserRoleService;

    /**
     *
     * @param userId
     * @return
     */
    @Override
    public Map<String, Object> findRoleDataByUserId(Long userId) {
        //1，从 sys_role 表中查询所有角色，返回list集合
        List<SysRole> allRolesList = baseMapper.selectList(null);
        //2，从 sys_user_role 表中根据userId查询 角色用户关系，查询userId对应所有角色id
        LambdaQueryWrapper<SysUserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserRole::getUserId, userId);
        //2.1 先获取userId所对应的所有角色信息
        List<SysUserRole> existUserRoleList = sysUserRoleService.list(wrapper);
        //从查询出来的用户id对应角色list集合，获取所有角色id
        //传统写法
        //ArrayList<Long> list = new ArrayList<>();
        //          for (SysUserRole sysUserRole : existUserRoleList) {
        //              Long roleId = sysUserRole.getRoleId();
        //              list.add(roleId);
        //      }

        //stream流写法
        //从查询出来的用户id对应角色list集合，获取所有角色id

        //2.2 从SysUserRole集合中获取所有的的roleId
        List<Long> existRoleIdList = existUserRoleList
                .stream()
//                .map(SysUserRole::getRoleId)
                .map(c -> c.getRoleId())
                .collect(Collectors.toList());
        //2.3，根据查询所有角色id，找到对应角色信息
        //根据角色id找到所有的角色的list集合进行比较
        List<SysRole> assignRoleList = new ArrayList<>();
        for (SysRole sysRole : allRolesList) {
            //sys_user_role中的id和sys_role中的id进行比较，相同就存入assignRoleList集合
            if (existRoleIdList.contains(sysRole.getId())) {
                assignRoleList.add(sysRole);
            }
        }
        //4，把得到两部分数据封装到map集合中
        Map<String, Object> roleMap = new HashMap<>();
//       返回匹配的角色
        roleMap.put("assignRoleList", assignRoleList);
//       返回所有角色
        roleMap.put("allRolesList", allRolesList);
        return roleMap;
    }

    @Transactional
    @Override
    public void doAssign(AssignRoleVo assignRoleVo) {
        //把用户之前分配角色数据删除，用户角色关系表，根据userId删除
        LambdaQueryWrapper<SysUserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserRole::getUserId, assignRoleVo.getUserId());
        sysUserRoleService.remove(wrapper);
        //重新进行分配
        List<Long> roleIdList = assignRoleVo.getRoleIdList();
        for (Long roleId : roleIdList) {
            if (StringUtils.isEmpty(roleId)) {
                continue;
            }
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(assignRoleVo.getUserId());
            sysUserRole.setRoleId(roleId);
            sysUserRoleService.save(sysUserRole);
        }
    }
}
